<?php

namespace app\common\lib;

class Time
{

    public static function userLoginExpiresTime($type = 2)
    {
        $type = !in_array($type, [1, 2]) ? 2 : $type;
        if ($type == 1) {
            $day = $type * 7;
        } else {
            $day = $type * 30;
        }
        return $day * 24 * 3600;
    }

    public static function formatTheTime($timestamp,$unit='s'){
        if($timestamp!==0&&empty($timestamp)){
            return '';
        }
        switch ($unit){
            case 'y':
                return date('Y',$timestamp);
            case 'm':
                return date('Y-m',$timestamp);
            case 'd':
                return date('Y-m-d',$timestamp);
            case 'h':
                return date('Y-m-d H',$timestamp);
            case 'i':
                return date('Y-m-d H:i',$timestamp);
            case 's':
                return date('Y-m-d H:i:s',$timestamp);
            default:
                return '';
        }
    }

    //获取最近15天的日期
    public static function nearDate($time = '', $format = 'Y-m-d')
    {
        $time = $time != '' ? $time : time(); //三元表达式取当前时间戳
        //循环
        $date = [];
        for ($i = 0; $i <= 14; $i++) {
            $date[$i] = date($format, strtotime('+' . $i - 15 . ' days', $time));
        }
        return $date;
    }
}